package com.example.recyclerview01.day05;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.recyclerview01.R;
import com.example.recyclerview01.baseuse.GridLayoutItemDecoration;
import com.example.recyclerview01.baseuse.RecyclerAdapter;
import com.example.recyclerview01.wrapper.WrapRecyclerView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * 长按后，才能激活拖动item。
 * https://blog.csdn.net/z240336124/article/details/53992120
 */
public class DragUseActivity extends AppCompatActivity {

    private RecyclerAdapter mListAdapter;
    RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_use);
        initData();

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);

        mRecyclerView.setLayoutManager(mLayoutManager);
        mListAdapter = new RecyclerAdapter(this, mData);
        mRecyclerView.setAdapter(mListAdapter);
        // 添加点击事件。
        mListAdapter.setOnItemClickListener(new RecyclerAdapter.ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(DragUseActivity.this, "item clicked:" + mData.get(position), Toast.LENGTH_SHORT).show();
                mData.remove(position);
                mListAdapter.notifyDataSetChanged(); // 回调观察者的onChanged方法
            }
        });


        // 实现向左侧滑删除 和拖行排序。
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                // 左右滑动删除，dragFlags传0.
                int swipeFlags = ItemTouchHelper.LEFT; // 侧滑
                int dragFlags = 0; // 拖动
                // GridLayoutManager 继承自 LinearLayoutManager。所以这2者instanceof 都是true.
                if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
                    // GridView 样式四个方向都可以
                    dragFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT |
                            ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                } else {
                    // ListView 样式不支持左右，只支持上下
                    dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                }
                return makeMovementFlags(dragFlags, swipeFlags);
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                // 拖动过程中，不断回调. 不断地替换位置，找到当前的位置，替换原来的位置。
                int fromPosition = viewHolder.getAdapterPosition();
                int targetPosition = target.getAdapterPosition();
                // 数据并没有变化  Java基础
                if (fromPosition > targetPosition) {
                    for (int i = fromPosition; i < targetPosition; i++) {
                        Collections.swap(mData, i, i + 1);// 改变实际的数据集
                    }
                } else {
                    for (int i = fromPosition; i > targetPosition; i--) {
                        Collections.swap(mData, i, i - 1);// 改变实际的数据集
                    }
                }
                String temp = mData.get(fromPosition);
                mData.set(fromPosition, mData.get(targetPosition));
                mData.set(targetPosition, temp);
                Log.d("1027", mData.toString()); // TODO. 交换item。修改数据集合。
                mListAdapter.notifyItemMoved(fromPosition, targetPosition);
                return true;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                // 侧滑删除执行完毕。回调的方法. 移除或更新Adapter.
                int currentSwipePos = viewHolder.getAdapterPosition();
                mData.remove(currentSwipePos);
                mListAdapter.notifyItemRemoved(currentSwipePos); // 带动画效果
            }

            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                // 状态发生改变：拖动状态，侧滑状态。
                if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
                    // 滑动或拖动的时候，背景设置为灰色。
                    viewHolder.itemView.setBackgroundColor(Color.GRAY);
                }
            }

            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder
                    viewHolder) {
                // 动画执行完毕，回到正常状态时回调。
                viewHolder.itemView.setBackgroundColor(0);
                ViewCompat.setTranslationX(viewHolder.itemView, 0);
            }
        });
        // 绑定RecyclerView。实现侧滑删除，拖动移动item排序功能。
        itemTouchHelper.attachToRecyclerView(mRecyclerView);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_item, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_gridview:
                mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
                break;
            case R.id.menu_listview:
                mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private List<String> mData; // 数据源

    protected void initData() {
        mData = new ArrayList<String>();
        for (int i = 'A'; i <= 'z'; i++) {
            mData.add("" + (char) i);
        }
        Log.d("TAG", "length:" + mData.size());
    }

}